package com.smyh.UIM.record;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.smyh.UIM.HostFragment;
import com.smyh.UIM.MainActivity;
import com.smyh.UIM.R;
import com.smyh.cache.UIM.MainCahce;
import com.smyh.common.MinaClientThread;
import com.smyh.protcol.device.CMDUtil;

import org.apache.mina.core.buffer.IoBuffer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.LineChartView;

/**
 * Created by Administrator on 2016/7/27.
 */
public class DeviceSyncRecordFm extends Fragment{

    public static Map<String,List<Integer>> syncRecordElecMap = new HashMap<>();
    public static Map<String,List<Integer>> syncRecordVoltMap = new HashMap<>();

    /**
     * 指示器参数 1组件
     */

    public Handler handler;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_record_device_sync,container,false);

        initView(view);
        initEvent();

        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
            }
        };
        return view;
    }

    private void initEvent() {

    }
    static LineChartView chart;
    static  Axis axisX;
    static  LineChartData data;
    static  Line line;
    static  Line line2;
    static List<Line> lines = new ArrayList<>();
    private void initView(View view){
        chart = (LineChartView) view.findViewById(R.id.chart);
        chart.setInteractive(true);
        //设置行为属性，支持缩放、滑动以及平移
        chart.setInteractive(true);
        chart.setZoomType(ZoomType.HORIZONTAL);
        chart.setVisibility(View.VISIBLE);
        // 设置线的颜色为蓝色，曲线光滑，显示标注 是否显示圆点  是否点击坐标提示
        line = new Line().setColor(Color.BLUE).setCubic(true).setHasLabels(false).setHasPoints(true).setHasLabelsOnlyForSelected(true);
        line2 = new Line().setColor(Color.RED).setCubic(true).setHasLabels(false).setHasPoints(true).setHasLabelsOnlyForSelected(true);
        data = new LineChartData();
        data.setValueLabelBackgroundColor(Color.TRANSPARENT);//此处设置坐标点旁边的文字背景
        data.setValueLabelBackgroundEnabled(false);
        data.setValueLabelsTextColor(Color.RED);  //此处设置坐标点旁边的文字颜色

        //坐标轴
        axisX = new Axis(); //X轴
        axisX.setHasTiltedLabels(true);
        axisX.setTextColor(Color.BLUE);
        axisX.setName("采集时间");
        axisX.setMaxLabelChars(10);
        Button syncElecBtn = (Button) view.findViewById(R.id.syncElecBtn);
        Button syncVoltBtn = (Button) view.findViewById(R.id.syncVoltBtn);
        syncVoltBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(MinaClientThread.session!=null){
                    if(HostFragment.connection_btn.isChecked()){
                        byte type = (byte) 0xdd;
                        IoBuffer buf = CMDUtil.readWaveData(type);
                        MinaClientThread.session.write(buf);
                    }
                }
            }
        });
        syncElecBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(MinaClientThread.session!=null){
                    if(HostFragment.connection_btn.isChecked()){
                        byte type = (byte) 0xdc;
                        IoBuffer buf = CMDUtil.readWaveData(type);
                        MinaClientThread.session.write(buf);
                    }
                }
            }
        });

    }
    static Handler handler1 = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            Toast.makeText(MainActivity.context, "指示器读取同步录波成功", Toast.LENGTH_SHORT).show();
            List<PointValue> values = new ArrayList<>();
            List<PointValue> values2 = new ArrayList<>();
            List<AxisValue> axisValues = new ArrayList<>();
            for (int i = 0;syncRecordElecMap.get(MainCahce.chooseDeviceAddr)!=null&& i < syncRecordElecMap.get(MainCahce.chooseDeviceAddr).size() ; i++) {
                values.add(new PointValue(i, syncRecordElecMap.get(MainCahce.chooseDeviceAddr).get(i)));
                axisValues.add(new AxisValue(i).setLabel(i+""));//为每个对应的i设置相应的label(显示在X轴)
            }
            for(int i = 0;syncRecordVoltMap.get(MainCahce.chooseDeviceAddr)!=null&&i<syncRecordVoltMap.get(MainCahce.chooseDeviceAddr).size();i++){
                values2.add(new PointValue(i, (syncRecordVoltMap.get(MainCahce.chooseDeviceAddr).get(i))));
            }
            line2.setValues(values2);
            lines.add(line2);
            line.setValues(values);
            lines.add(line);
            data.setLines(lines);
            data.setAxisXBottom(axisX);
            axisX.setValues(axisValues);
            chart.setLineChartData(data);

            Viewport tempViewport = new Viewport(0, chart.getMaximumViewport().height(), 40, 0) ;
            chart.setCurrentViewport(tempViewport);
        }
    };
    public static void freshModel() {
        handler1.sendMessage(new Message());
    }

}
